{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# While循环\n",
    "- 一个循环语句\n",
    "- 表示当某个条件成立的时候，就循环\n",
    "- 不知道循环次数，但是可以确定循环的成立条件就用while循环\n",
    "- while语法：\n",
    "\n",
    "        while 条件表达式：\n",
    "                语句块\n",
    "                 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第1年可以拿多少106700.0钱\n",
      "第2年可以拿多少113848.9钱\n",
      "第3年可以拿多少121476.77629999998钱\n",
      "第4年可以拿多少129615.72031209998钱\n",
      "第5年可以拿多少138299.97357301068钱\n",
      "第6年可以拿多少147566.07180240238钱\n",
      "第7年可以拿多少157452.99861316333钱\n",
      "第8年可以拿多少168002.34952024528钱\n",
      "第9年可以拿多少179258.5069381017钱\n",
      "第10年可以拿多少191268.8269029545钱\n",
      "第11年可以拿多少204083.83830545243钱\n"
     ]
    }
   ],
   "source": [
    "# 如果说年利率是6.7%，则多少年后本钱会翻倍\n",
    "benqian = 100000\n",
    "year = 0 \n",
    "while benqian < 200000:\n",
    "    benqian = benqian * (1+0.067)\n",
    "    year += 1\n",
    "    print(\"第{0}年可以拿多少{1}钱\".format(year,benqian))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 函数\n",
    "- 代码的一种组织形式\n",
    "- 一个函数一般完成一项特定的功能\n",
    "- 函数使用\n",
    "    - 先定义一个函数\n",
    "    - 调用函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我是一个函数\n",
      "我要完成一个功能\n",
      "我要结束了\n"
     ]
    }
   ],
   "source": [
    "# 定义函数\n",
    "# 1. def关键字 后跟一个空格\n",
    "# 2. 函数名称  不能使用关键字\n",
    "# 3. 括号冒号不能省略，括号内可以有参数\n",
    "# 4. 函数内所有代码 缩进\n",
    "def func():\n",
    "    print(\"我是一个函数\")\n",
    "    print(\"我要完成一个功能\")\n",
    "    print(\"我要结束了\")\n",
    "    \n",
    "# 函数调用    \n",
    "func()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 函数的参数和返回值\n",
    "- 参数：负责给函数传递一些必要的数据\n",
    "    - 形参： 在函数定义的时候用到的参数没有具体值，是一个占位符号\n",
    "    - 实参： 在调用的时候输入的值\n",
    "- 返回值：函数的执行结果\n",
    "    - 使用return关键字\n",
    "    - 没有返回值（return），默认返回一个None\n",
    "    - 函数一旦执行return语句，函数就结束了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "zhangsan,吃饭了吗\n",
      "Sir,你的车坏了\n"
     ]
    }
   ],
   "source": [
    "# 参数的定义和使用\n",
    "def hello(person):\n",
    "    print(\"{0},吃饭了吗\".format(person))\n",
    "    print(\"Sir,你的车坏了\")\n",
    "\n",
    "# 函数调用 入参数    \n",
    "hello(\"zhangsan\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "zhangsan,吃饭了吗\n",
      "我吃了哦\n",
      "zhangsan,吃饭了吗\n",
      "我吃了哦\n"
     ]
    }
   ],
   "source": [
    "# return语句的基本使用 一\n",
    "def hello(person):\n",
    "    print(\"{0},吃饭了吗\".format(person))\n",
    "    return \"我吃了哦\"\n",
    "\n",
    "# 函数调用 入参数   +  返回值  \n",
    "print(hello(\"zhangsan\"))\n",
    "\n",
    "\n",
    "# return语句的基本使用 二\n",
    "def helloEr(person):\n",
    "    print(\"{0},吃饭了吗\".format(person))\n",
    "    return \"我吃了哦\"\n",
    "    print(\"我不会被打印哦哦哦\")\n",
    "    \n",
    "# 函数调用 \n",
    "print(helloEr(\"zhangsan\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function print in module builtins:\n",
      "\n",
      "print(...)\n",
      "    print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n",
      "    \n",
      "    Prints the values to a stream, or to sys.stdout by default.\n",
      "    Optional keyword arguments:\n",
      "    file:  a file-like object (stream); defaults to the current sys.stdout.\n",
      "    sep:   string inserted between values, default a space.\n",
      "    end:   string appended after the last value, default a newline.\n",
      "    flush: whether to forcibly flush the stream.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 查找函数的使用文档\n",
    "# 1. 用help函数\n",
    "help(print)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 * 1 = 1 2 * 1 = 2 2 * 2 = 4 3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36 7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49 8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64 9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81 "
     ]
    }
   ],
   "source": [
    "# 九九乘法表\n",
    "for row in range(1,10):\n",
    "    # 打印一行\n",
    "#     print(\"{0} line\".format(row))\n",
    "    for col in range(1,row+1):\n",
    "        # print函数默认是打印后换行\n",
    "        print(\"{0} * {1} = {2}\".format(row,col,row * col) ,end = \" \")    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 参数详解\n",
    "- 参数分类\n",
    "    - 普通参数\n",
    "    - 默认参数\n",
    "    - 关键字参数\n",
    "    - 收集参数\n",
    "- 普通参数\n",
    "    - 参见上例\n",
    "    - 定义的时候直接定义变量名\n",
    "    - 调用的时候直接把变量或者值放入指定位置\n",
    "    \n",
    "    \n",
    "                    def 函数名（参数1，参数2，....）:\n",
    "                        函数体\n",
    "                    \n",
    "                    # 调用\n",
    "                    函数体（value1，value2，....）\n",
    "                    \n",
    "                    # 调用的时候，具体值参考的是位置，按位置赋值\n",
    "                    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "张三 is 19, and he is good student\n",
      "张三 is 19, and she is good student\n"
     ]
    }
   ],
   "source": [
    "# 默认参数示例\n",
    "# 报名函数，要知道学生的性别\n",
    "# 学习python的学生基本是男，所以报名的时候没有特别指定，那就默认是男性\n",
    "\n",
    "def reg(name,age,gender = \"male\"):\n",
    "    if gender == \"male\":\n",
    "        print(\"{0} is {1}, and he is good student\".format(name,age))\n",
    "    else:\n",
    "        print(\"{0} is {1}, and she is good student\".format(name,age))\n",
    "\n",
    "        \n",
    "#调用\n",
    "reg(\"张三\",19)\n",
    "reg(\"张三\",19,\"female\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
